package com.ruoyi.project.dj.service.impl;

import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.dj.mapper.DjStudyRecordMapper;
import com.ruoyi.project.dj.domain.DjStudyRecord;
import com.ruoyi.project.dj.service.IDjStudyRecordService;

/**
 * 学习记录Service业务层处理
 *
 * @author ruoyi
 * @date 2025-09-15
 */
@Service
public class DjStudyRecordServiceImpl implements IDjStudyRecordService
{
    @Autowired
    private DjStudyRecordMapper djStudyRecordMapper;

    /**
     * 查询学习记录
     *
     * @param id 学习记录ID
     * @return 学习记录
     */
    @Override
    public DjStudyRecord selectDjStudyRecordById(Long id)
    {
        return djStudyRecordMapper.selectDjStudyRecordById(id);
    }

    /**
     * 查询学习记录列表
     *
     * @param djStudyRecord 学习记录
     * @return 学习记录
     */
    @Override
    public List<DjStudyRecord> selectDjStudyRecordList(DjStudyRecord djStudyRecord)
    {
        return djStudyRecordMapper.selectDjStudyRecordList(djStudyRecord);
    }

    /**
     * 新增学习记录
     *
     * @param djStudyRecord 学习记录
     * @return 结果
     */
    @Override
    public int insertDjStudyRecord(DjStudyRecord djStudyRecord)
    {
        return djStudyRecordMapper.insertDjStudyRecord(djStudyRecord);
    }

    /**
     * 修改学习记录
     *
     * @param djStudyRecord 学习记录
     * @return 结果
     */
    @Override
    public int updateDjStudyRecord(DjStudyRecord djStudyRecord)
    {
        return djStudyRecordMapper.updateDjStudyRecord(djStudyRecord);
    }

    /**
     * 删除学习记录对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteDjStudyRecordByIds(Long[] ids)
    {
        return djStudyRecordMapper.deleteDjStudyRecordByIds(ids);
    }

    /**
     * 删除学习记录信息
     *
     * @param id 学习记录ID
     * @return 结果
     */
    @Override
    public int deleteDjStudyRecordById(Long id)
    {
        return djStudyRecordMapper.deleteDjStudyRecordById(id);
    }

    /**
     * 根据用户ID、资源类型和资源ID查询学习记录
     *
     * @param memberId 党员ID
     * @param resourceType 资源类型
     * @param resourceId 资源ID
     * @return 学习记录
     */
    @Override
    public DjStudyRecord selectDjStudyRecordByMemberAndResource(Long memberId, String resourceType, Long resourceId)
    {
        return djStudyRecordMapper.selectDjStudyRecordByMemberAndResource(memberId, resourceType, resourceId);
    }

    /**
     * 开始学习记录（创建或更新记录）
     *
     * @param memberId 党员ID
     * @param resourceType 资源类型
     * @param resourceId 资源ID
     * @return 结果
     */
    @Override
    public int startStudyRecord(Long memberId, String resourceType, Long resourceId)
    {
        // 先查询是否已存在记录
        DjStudyRecord existRecord = djStudyRecordMapper.selectDjStudyRecordByMemberAndResource(memberId, resourceType, resourceId);
        
        if (existRecord == null) {
            // 创建新记录
            DjStudyRecord record = new DjStudyRecord();
            record.setMemberId(memberId);
            record.setResourceType(resourceType);
            record.setResourceId(resourceId);
            record.setDuration(0);
            record.setProgress(0);
            record.setIsComplete(0);
            record.setLastPosition(0);
            record.setCollectStatus(0);
            record.setLastStudyTime(new Date());
            record.setCreateTime(new Date());
            return djStudyRecordMapper.insertDjStudyRecord(record);
        } else {
            // 更新学习时间
            existRecord.setLastStudyTime(new Date());
            existRecord.setUpdateTime(new Date());
            return djStudyRecordMapper.updateDjStudyRecord(existRecord);
        }
    }

    /**
     * 更新学习时长
     *
     * @param memberId 党员ID
     * @param resourceType 资源类型
     * @param resourceId 资源ID
     * @param durationIncrement 增加的学习时长(秒)
     * @param currentPosition 当前学习位置(秒，可选)
     * @return 结果
     */
    @Override
    public int updateStudyDuration(Long memberId, String resourceType, Long resourceId, 
                                   Integer durationIncrement, Integer currentPosition)
    {
        return djStudyRecordMapper.updateStudyDurationAndPosition(memberId, resourceType, resourceId, 
                                                                  durationIncrement, currentPosition);
    }

    /**
     * 结束学习记录
     *
     * @param memberId 党员ID
     * @param resourceType 资源类型
     * @param resourceId 资源ID
     * @param finalDuration 最终学习时长(秒)
     * @param finalPosition 最终学习位置(秒，可选)
     * @param isComplete 是否完成学习
     * @return 结果
     */
    @Override
    public int endStudyRecord(Long memberId, String resourceType, Long resourceId, 
                              Integer finalDuration, Integer finalPosition, boolean isComplete)
    {
        DjStudyRecord record = djStudyRecordMapper.selectDjStudyRecordByMemberAndResource(memberId, resourceType, resourceId);
        if (record != null) {
            if (finalDuration != null) {
                record.setDuration(finalDuration);
            }
            if (finalPosition != null) {
                record.setLastPosition(finalPosition);
            }
            record.setIsComplete(isComplete ? 1 : 0);
            record.setLastStudyTime(new Date());
            record.setUpdateTime(new Date());
            return djStudyRecordMapper.updateDjStudyRecord(record);
        }
        return 0;
    }
}